import React, { memo } from 'react';
import { Button, Form, Radio, Input, Switch, Row, Col } from 'antd';
import { SelectRecommendPost } from '@/pages/Recruitment/containers/select';
// import { history } from 'umi';
import styles from './index.less';

const evaluationFormLayout = {
    wrapperCol: { span: 18 },
    labelCol: { span: 6 }
};
// const limitDecimals = (values = 3) => {
//     if (values) {
//         let val = parseInt(values, 10);
//         if (val > 10) {
//             val = 10;
//         }
//         return val;
//     }
//     return '';
// };
const { TextArea } = Input;
// const handleCancel = () => {
//     history.goBack();
// };
const Evaluation = ({
    isLastStep = false,
    form,
    onSubmit,
    resumeId,
    keyPrefix = '',
    defaultRecommendValue,
    ...rest
}) => {
    const { getFieldDecorator, getFieldValue } = form;
    const [switchVisible, setSwitchVisible] = React.useState(false);
    const [recommendExist, setRecommendExist] = React.useState(false);

    const resultType = form.getFieldValue(`${keyPrefix}resultType`);
    const switchCheck = form.getFieldValue('resultRecommendCheck');

    /**
     * 渲染背调相关
     */
    const renderBackgroundCheck = () => {
        if (isLastStep && getFieldValue(`${keyPrefix}resultType`) === 4) {
            return (
                <>
                    <Form.Item label="发起背调：">
                        {getFieldDecorator(`${keyPrefix}needBackgroundFlag`, {
                            initialValue: false,
                            valuePropName: 'checked'
                        })(<Switch style={{ marginLeft: 8 }} />)}
                    </Form.Item>
                    {/* {getFieldValue(`${keyPrefix}needBackgroundFlag`) && (
                        <Form.Item label="背调人数：">
                            {getFieldDecorator(`${keyPrefix}backgroundNum`, {
                                initialValue: 3,
                                rules: [{ required: true, message: '请输入背调人数！' }]
                            })(
                                <InputNumber
                                    formatter={limitDecimals}
                                    parser={limitDecimals}
                                    min={1}
                                    max={10}
                                    placeholder="请输入背调人数"
                                />
                            )}
                        </Form.Item>
                    )} */}
                </>
            );
        }
        return null;
    };

    React.useEffect(() => {
        setSwitchVisible(resultType === 5);
    }, [resultType]);

    React.useEffect(() => {
        if (!switchCheck) {
            form.resetFields([`${keyPrefix}recommendRecruitId`]);
        } else if (defaultRecommendValue) {
            form.setFieldsValue({ [`${keyPrefix}recommendRecruitId`]: defaultRecommendValue });
        }
    }, [switchCheck, defaultRecommendValue]);

    return (
        <Form {...evaluationFormLayout} className={styles.evaluationFormWrap} {...rest}>
            <Form.Item label="面试结果">
                {getFieldDecorator(`${keyPrefix}resultType`, {
                    initialValue: 4
                })(
                    <Radio.Group>
                        <Radio value={4}>通过</Radio>
                        <Radio value={6}>需复试</Radio>
                        <Radio value={5}>淘汰</Radio>
                    </Radio.Group>
                )}
            </Form.Item>
            {switchVisible && (
                <Form.Item label="推荐到其他岗位">
                    <Row gutter={10} type="flex">
                        <Col>{getFieldDecorator('resultRecommendCheck')(<Switch />)}</Col>
                        <Col>
                            {getFieldDecorator(`${keyPrefix}recommendRecruitId`)(
                                <SelectRecommendPost
                                    resumeId={resumeId}
                                    disabled={!switchCheck}
                                    callbackForExist={setRecommendExist}
                                />
                            )}
                        </Col>
                    </Row>
                </Form.Item>
            )}
            <Form.Item label=" " colon={false} required={false}>
                {getFieldDecorator(`${keyPrefix}resultContent`, {
                    initialValue: '',
                    rules: [
                        { required: true, message: '请填写综合评价' },
                        { max: 1000, message: '综合评价不能超过1000个字符' }
                    ]
                })(
                    <TextArea
                        placeholder="请输入综合评价"
                        style={{ width: 355 }}
                        autoSize={{ minRows: 3, maxRows: 3 }}
                    />
                )}
            </Form.Item>
            {renderBackgroundCheck()}
            <Form.Item label=" " colon={false}>
                <Button
                    type="primary"
                    disabled={recommendExist && resultType === 5}
                    className={styles.formSubmitButton}
                    onClick={onSubmit}
                >
                    确定
                </Button>
                {/* {isLastStep && (
                    <Button style={{ marginLeft: '10px' }} onClick={handleCancel}>
                        取消
                    </Button>
                )} */}
            </Form.Item>
        </Form>
    );
};
export default memo(Evaluation);
